﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskMessage;

namespace TaskMessage.Mail
{
    /// <summary>
    /// 邮件发送管理
    /// </summary>
    public class SendMessageMail:ISendMessage
    {
        /*
        /// <summary>
        /// 读取邮件
        /// </summary>
        /// <param name="topnum"></param>
        /// <returns></returns>
        public List<MessageEntity> messageList(int topnum)
        {

            IData data = (IData)Activator.CreateInstance("TaskMessage.Mail.Data", string.Format("TaskMessage.Mail.Data.{0}", MailConfig.Instance.DBClassName)).Unwrap();

            return data.messageList(topnum);


            List<MessageEntity> lst = new List<MessageEntity>();

            MessageEntity m = new MessageEntity();
            m.FormID = "";
            m.FormName = "";
            m.ID = "1";
            m.MessageTime = DateTime.Now;
            m.Title = "中华人民共和国";
            m.ToID = "test37211@126.com";
            m.ToName = "鄂尔多斯";
            m.Txt = "<b>测试邮件内容</b>";
            lst.Add(m);

            return lst;

        }
        */

        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool sendMessage(MessageEntity m)
        {
            bool rBool = false;

            Email mail = new Email();
            mail.host = MailConfig.Instance.smtp;
            mail.hostauth = Convert.ToBoolean(MailConfig.Instance.auth);
            mail.hostssl = Convert.ToBoolean(MailConfig.Instance.smtpssl);
            mail.isbodyHtml = true;
            mail.mailBody = m.Txt;
            mail.mailFrom = MailConfig.Instance.formname;
            mail.mailFromaddress = MailConfig.Instance.formid;
            mail.mailPwd = MailConfig.Instance.authUserpwd;
            mail.mailSubject = m.Title;
            mail.mailToArray = new Dictionary<string, string>();
            mail.mailToArray.Add(m.ToID, m.ToName);
            mail.mailUserName = MailConfig.Instance.authUserName;
            rBool = mail.Send();

            if (Convert.ToInt32(MailConfig.Instance.sendsleeptime) >= 0)
            {

                System.Threading.Thread.Sleep(Convert.ToInt32(
                                                         MailConfig.Instance.sendsleeptime));
            }
            return rBool;
        }

        /*
        /// <summary>
        /// 列新消息发送状态
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="status">消息状态 0 未发送 1:发送中 2:发送成功 3:发送失败</param>
        /// <returns></returns>
        public bool updateMessageStatus(string id,int status)
        {
            //bool rBool = false;

            //return rBool;

            IData data = (IData)Activator.CreateInstance("TaskMessage.Mail.Data", string.Format("TaskMessage.Mail.Data.{0}", MailConfig.Instance.DBClassName)).Unwrap();

            return data.updateMessageStatus(id,status);
        }
         */
    }
}
